'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const { pages } = require('./build/pages') // 多页面配置
const CompressionWebpackPlugin = require('compression-webpack-plugin') // gzip插件

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
const port = 8080

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: './',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  runtimeCompiler: false,
  pages,
  devServer: {
    port: port,
    open: true,
    https: false,
    overlay: { // 编译错误显示
      warnings: false,
      errors: true
    },
    proxy: {
      '/webSocket': {
        target: 'ws://192.168.101.16:9000', // 开发环境
        ws: true, // 代理的WebSockets
        changeOrigin: true, // 需要虚拟主机站点
        pathRewrite: {
          '^/webSocket': '/webSocket'
        }
      },
      '/data': {
        target: '', // 开发环境
        ws: false, // 代理的WebSockets
        changeOrigin: true // 需要虚拟主机站点
      },
      '/': {
        // target: 'http://172.16.2.183:9000/', // 开发环境
        target: 'http://192.168.101.16:9000',
        // target: "http://172.16.2.69:9000",
        ws: false, // 代理的WebSockets
        changeOrigin: true, // 需要虚拟主机站点
        pathRewrite: {
          '^/': '/'
        }
      }
    }
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [new CompressionWebpackPlugin(
      {
        test: /\.(js|css|json|txt)(\?.*)?$/i, // 处理所有匹配此 {RegExp} 的资源
        threshold: 10240, // 只处理比这个值大的资源。按字节计算10K以上进行压缩
        minRatio: 0.8 // 只有压缩率比这个值小的资源才会被处理
      }
    )]
  },
  chainWebpack(config) {
    // 移除 prefetch 插件
    config.plugins.delete('prefetch-index')
    // 移除 preload 插件
    config.plugins.delete('preload-index')
    config.optimization.splitChunks({
      chunks: 'all',
      cacheGroups: {
        libs: {
          name: 'chunk-libs',
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: 'initial' // only package third parties that are initially dependent
        },
        elementUI: {
          name: 'chunk-elementUI', // split elementUI into a single package
          priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
        },
        commons: {
          name: 'chunk-commons',
          test: resolve('src/components'), // can customize your rules
          minChunks: 3, //  minimum common number
          priority: 5,
          reuseExistingChunk: true
        }
      }
    })
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        path.resolve(__dirname, './src/styles/variables.scss')
      ]
    }
  }
}
